//
//  LBMultiLevCityView.m
//  app
//
//  Created by 杨庆人 on 16/12/23.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBMultiLevCityView.h"
#import <LBCategory/LBCategory.h>
#import <Masonry/Masonry.h>

@interface LBMultiLevCityView ()

@property (nonatomic,strong)UIImageView * provbindView;
@property (nonatomic,strong)UIImageView * citybindView;
@property (nonatomic,strong)UIImageView * areabindView;

@property (nonatomic,assign)LBCityPickerViewType cityPickerViewType;

@property (nonatomic,copy)lbCityPickViewChoseBlock cityChoseBlock;

@end

@implementation LBMultiLevCityView

- (LBUnderlineBtn *)provBtn {
    if (!_provBtn) {
        _provBtn = ({
            LBUnderlineBtn * button = [[LBUnderlineBtn alloc] init];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(selectCityBtnClick) forControlEvents:UIControlEventTouchUpInside];
            
            button;        
        });
    }
    return _provBtn;
}

- (UIImageView *)provbindView {
    if (!_provbindView) {
        _provbindView = ({
            UIImageView * combinedView = [[UIImageView alloc] init];
            combinedView.image = [UIImage imageNamed:@"LBFormView.bundle/Combined Shape"];
            
            combinedView;
        });
    }
    return _provbindView;
}
- (LBUnderlineBtn *)cityBtn {
    if (!_cityBtn) {
        _cityBtn = ({
            LBUnderlineBtn * button = [[LBUnderlineBtn alloc] init];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(selectCityBtnClick) forControlEvents:UIControlEventTouchUpInside];
            
            button;
        });
    }
    return _cityBtn;
}

- (UIImageView *)citybindView {
    if (!_citybindView) {
        _citybindView = ({
            UIImageView * combinedView = [[UIImageView alloc] init];
            combinedView.image = [UIImage imageNamed:@"LBFormView.bundle/Combined Shape"];
            
            combinedView;
        });
    }
    return _citybindView;
}
- (LBUnderlineBtn *)areaBtn {
    if (!_areaBtn) {
        _areaBtn = ({
            LBUnderlineBtn * button = [[LBUnderlineBtn alloc] init];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(selectCityBtnClick) forControlEvents:UIControlEventTouchUpInside];
            
            button;
        });
    }
    return _areaBtn;
}

- (UIImageView *)areabindView {
    if (!_areabindView) {
        _areabindView = ({
            UIImageView * combinedView = [[UIImageView alloc] init];
            combinedView.image = [UIImage imageNamed:@"LBFormView.bundle/Combined Shape"];
            
            combinedView;
        });
    }
    return _areabindView;
}
- (instancetype)initWithCityPickerViewType:(LBCityPickerViewType)cityPickerViewType cityChoseBlock:(lbCityPickViewChoseBlock)cityChoseBlock {
    if (self = [super init]) {
        _cityChoseBlock = cityChoseBlock;
        _cityPickerViewType = cityPickerViewType;
        [self setMultiLevCityView];
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];

    [self.provBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self);
    }];
    [self.provbindView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.provBtn.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(10, 5));
        make.centerY.mas_equalTo(self);
    }];
    [self.cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.provbindView.mas_right).offset(15);
    }];
    [self.citybindView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.cityBtn.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(10, 5));
        make.centerY.mas_equalTo(self);
    }];
    [self.areaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.citybindView.mas_right).offset(15);
    }];
    [self.areabindView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.areaBtn.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(10, 5));
        make.centerY.mas_equalTo(self);
    }];
}
- (void)setMultiLevCityView {
    [self addSubview:self.provBtn];
    [self addSubview:self.provbindView];
    [self addSubview:self.cityBtn];
    [self addSubview:self.citybindView];
    [self addSubview:self.areaBtn];
    [self addSubview:self.areabindView];
    
    if (_cityPickerViewType == LBCityPickerViewTypeProvincialCity) {
        self.areaBtn.hidden = self.areabindView.hidden = YES;
    }
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectCityBtnClick)];
    
    [self addGestureRecognizer:tap];
}

- (void)selectCityBtnClick {
    
    [[UIApplication getCurrentViewController].view endEditing:YES];
    __weak typeof(self)weakSelf = self;
    [LBCityPickerView showCityPickViewByAnimation:YES LBCityPickerViewType:_cityPickerViewType Complete:^(LBCityMsgModel *selectedProvinceModel, LBCityMsgModel *selectedCityModel, LBCityMsgModel *selectedAreaModel) {
        
        [weakSelf.provBtn setTitle:selectedProvinceModel.areaName forState:UIControlStateNormal];
        [weakSelf.cityBtn setTitle:selectedCityModel.areaName forState:UIControlStateNormal];
        [weakSelf.areaBtn setTitle:selectedAreaModel.areaName forState:UIControlStateNormal];

        !weakSelf.cityChoseBlock ? :weakSelf.cityChoseBlock(selectedProvinceModel,selectedCityModel,selectedAreaModel);
    }];
}

@end


@interface LBUnderlineBtn ()

@property (nonatomic,strong)UIImageView * underline;

@end
@implementation LBUnderlineBtn

- (instancetype)init {
    if (self = [super init]) {
        [self setUnderline];
    }
    return self;
}

- (UIImageView *)underline{
    if (!_underline) {
        _underline = [[UIImageView alloc] init];
        _underline.backgroundColor = [UIColor colorWithR:218 G:218 B:218 A:1];
    }
    return _underline;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    [self.underline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(1);
    }];
}
- (void)setUnderline {
    [self addSubview:self.underline];
}
@end
